<?php

class Ead_MateriaisController extends Ead_Controller_Action
{
    public  $dir        = 'public/upload/enviodearquivo/';
    public  $_file_col  = 'arquivo';
    
    public function init()
    {
        //if($this->getRequest()->getActionName() == 'edit') $this->form = new Sistema_Form_Sobre();
    }

    public function indexAction() {
        
        Zend_Loader::loadClass('datetools','library');
        Zend_Loader::loadClass('imagem','library');
        Zend_Loader::loadClass('dominio','library');
        
        $this->view->headScript()->appendFile($this->_urlbase.'/public/media/js/ead/materiais.js?1=3');
        
        $sessao = new Zend_Session_Namespace('Aluno');
        
        $query = 'SELECT SQL_CACHE DISTINCT p.PRS_ID, p.PRS_NOM, d.DCP_ID, d.DCP_NOM '
                .' FROM PROFESSOR p, DISCIPLINA d, TOPICO t, TOPICO_ALUNO a '
                .' WHERE p.DCP_ID = d.DCP_ID AND p.PRS_STA = 1 AND d.DCP_STA = 1 '
                .' AND p.PRS_ID = t.PRS_ID AND t.TPC_ID = a.TPC_ID '
                .' AND a.ALU_ID = '.$sessao->aluno['ALU_ID']
                .' ORDER BY PRS_NOM';
        $result = $this->db->fetchAll($query); 
        $this->view->menu = $result;
          
        $where = '';
        
        $dcp_id = $this->_getParam('dcp_id', 0);
        $prs_id = $this->_getParam('prs_id', 0);
        
        if($dcp_id > 0){ 
            $where = ' AND p.DCP_ID = '.$dcp_id.' AND p.PRS_ID = '.$prs_id.' ';
        
        } else if(isset($result[0]['DCP_ID'])){     
            $where = ' AND p.DCP_ID = '.$result[0]['DCP_ID'].' AND p.PRS_ID = '.$result[0]['PRS_ID'].' ';
        } else{
            $where = ' AND p.DCP_ID = 0 AND p.PRS_ID = 0 ';
        }
        
        
        $query = 'SELECT SQL_CACHE DISTINCT p.PRS_NOM, d.DCP_NOM '
            .' FROM PROFESSOR p, DISCIPLINA d'
            .' WHERE p.DCP_ID = d.DCP_ID AND p.PRS_STA = 1 AND d.DCP_STA = 1 '
            . $where;

        $result = $this->db->fetchRow($query); 
        $this->view->dis = $result;        
        
        
        $query = 'SELECT SQL_CACHE t.TPC_ID,t.TPC_NOM '
                .' FROM TOPICO t, TOPICO_ALUNO ta, PROFESSOR p '
                .' WHERE t.TPC_ID = ta.TPC_ID AND ta.ALU_ID = '.$sessao->aluno['ALU_ID']
                .' AND t.TPC_STA = 1 AND t.PRS_ID = p.PRS_ID AND p.PRS_STA = 1 '
                . $where 
                .' ORDER BY TPC_ORD, TPC_NOM';
        $result = $this->db->fetchAll($query); 
        $this->view->topicos = $result;
        
        $this->view->ALU_ID = $sessao->aluno['ALU_ID'];
        
        // envio de redação
        if(strlen($_FILES[$this->_file_col]['name']) > 5){
            
            Zend_Loader::loadClass('imagem','library');
            
            $sessao = new Zend_Session_Namespace('Aluno');
            
            $letra = dominio::getLetra(0);
            
            $data = array(
                'EVA_NOM' => $_FILES[$this->_file_col]['name'],
                'ALU_ID' => $sessao->aluno['ALU_ID'],
                'EVA_DATHOR' => date('Y-m-d H:i:s'),
            );
            $this->db->insert('ENVIO_ARQUIVO',$data);
            $id = $this->db->lastInsertId();
            
            $returnImg = imagem::upload($this->_file_col,$this->dir,$id);
            imagem::rename($id.$letra,$this->dir,$returnImg['filenew']);
            
            echo '<script>alert("Enviado arquivo com sucesso!")</script>';
            
        }
        
        
        $s_turma = new Zend_Session_Namespace('Turma');

        $this->view->turma = $s_turma->turma;
        
        
    }
    
    public function playAction(){
        
        $id = $this->_getParam('id', 0);
        
        $tpc_id = $this->_getParam('tpc_id', 0);
        
        $this->registraAcessoConteudo($id,$tpc_id);
        
        $query = 'SELECT SQL_CACHE * FROM CONTEUDO WHERE CON_ID = ' . $id;
        $this->view->data = $this->db->fetchRow($query);
        
    }
    
    public function downloadAction(){
        
        $this->_helper->layout->disableLayout(); 
        $this->_helper->viewRenderer->setNoRender();
        
        $id = $this->_getParam('id', 0);
        
        $tpc_id = $this->_getParam('tpc_id', 0);
        
        $this->registraAcessoConteudo($id,$tpc_id);
        
        echo json_encode(array('ok' => TRUE));
    }
    
    public function registraAcessoConteudo($id,$tpc_id){
        $sessao = new Zend_Session_Namespace('Aluno');
        
        if($id > 0 && $tpc_id > 0){
            $query = 'SELECT SQL_CACHE CON_ID FROM ALUNO_CONTEUDO WHERE CON_ID = ' . $id.' AND TPC_ID = ' . $tpc_id.' AND ALU_ID = ' .$sessao->aluno['ALU_ID'];
            $row = $this->db->fetchRow($query);

            if(!isset($row['CON_ID'])){
                $data = array(
                    'TPC_ID'     => $tpc_id,
                    'CON_ID'     => $id,
                    'ALU_ID'     => $sessao->aluno['ALU_ID'],
                    'ALC_DATHOR' => date('Y-m-d H:i:s'),
                );
                $this->db->insert('ALUNO_CONTEUDO',$data);
            }   
        }
        
    }
}